﻿using System.Collections.Generic;
using Microsoft.Win32;

namespace ScottLane.MediaFeeder.ObjectModel
{
    public class MimeType
    {
        private static Dictionary<string, string> mimeTypes = new Dictionary<string,string>();

        public static string FromExtension(string extension)
        {
            string mimeType = "application/unknown";

            if (mimeTypes.ContainsKey(extension))
            {
                mimeType = mimeTypes[extension];
            }
            else
            {
                RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);

                if (key != null && key.GetValue("Content Type") != null)
                {
                    mimeType = key.GetValue("Content Type").ToString();
                    mimeTypes[extension] = mimeType;
                }
            }

            return mimeType;
        }
    }
}
